<?php
namespace app\common\model;
use think\Model;
use  think\Db;
/**
 * 知识点
 */
class Knowledge extends Model
{
    
    protected $append = [
        'exam1','exam2','subject_name'
    ];

    protected static function init()
    {
        Knowledge::event('after_delete', function ($knowledge) {
            Db::name('exam')->where('knowledge_id',$knowledge['id'])->delete();
        });
    }

    public function getExam1Attr($value,$data){
        return Db::name('exam')->where('knowledge_id',$data['id'])->where('type_id',1)->where('status',1)->count();
    }
    public function getExam2Attr($value,$data){
        return Db::name('exam')->where('knowledge_id',$data['id'])->where('type_id','>','1')->where('status',1)->count();
    }

    public function getSubjectNameAttr($value,$data){
        return Db::name('subject')->where('id',$data['subject_id'])->value('name');
    }
}